<script setup>
import api from '../api/car_api.js'
import {ref, onMounted} from "vue";
import {ElMessageBox} from "element-plus";
import lodash from 'lodash'

//定义绑定的数据
//表格中的数据
const tableData = ref([]);
const total=ref(0);
//定义对话框的显示状态
const editWinVisible = ref(false);
const addWinVisible=ref(false);
//查询数据
const searchObj = ref({
  current: 1,
  limit: 10,
  id:'',
  driver_id:'',
  car_number:'',
  car_model:'',
  car_color:'',
  car_brand:'',
  register_time:'',
});

//要修改或者新增的数据
const obj = ref({});
const search = async () => {
  try {
    var result = await api.getCarPage(searchObj.value.current, searchObj.value.limit, searchObj.value);
    tableData.value = result.records;
    total.value = result.total;
  } catch (error) {
    console.error('获取用户列表时出错:', error);
    tableData.value = [];
    total.value = 0;
  }
  //try代替了
  //var result = await api.getUserList(searchObj.value);
  //tableData.value = result;
  //total.value=result.count;
}

//页面加载后要挂载的数据
onMounted(async () => {
  await search();
});


//上传成功
const handleUploadSuccess = (response,file) => {
  console.log("-----------------");
  console.log(response);
  console.log(file);
  obj.value.avatar=response.data;
}//分页查询用
const handleSizeChange=()=>{
  search();
}//分页查询用
const handleCurrentChange=()=>{
  search();
}
//定义方法
const handlerSearch = () => {
  searchObj.value.current = 1;
  search();
}
const handlerAdd=()=>{//添加
  obj.value={};
  addWinVisible.value=true;//打开对话框
}
//新增对话框的取消按钮点击事件
const addCancel=()=>{
  obj.value={};//清空数据
  addWinVisible.value=false;//关闭新增对话框
}
//编辑对话框的取消按钮点击事件
const editCancel = () => {
  obj.value = {};//清空数据
  editWinVisible.value = false;//关闭对话框
}
//编辑对话框的提交按钮点击事件
const editCommit = async () => {
  await api.updateCar(obj.value);//提交修改数据
  await search();//刷新表格
  editWinVisible.value = false;//关闭编辑对话框
}
//新增对话框的提交按钮点击事件
const addCommit=async()=>{
  await api.addCar(obj.value);//提交新增数据
  await search();//刷新表格
  addWinVisible.value=false;//关闭新增对话框
}
const handleEdit = (index, row) => {
  obj.value=lodash.clone(row);
  editWinVisible.value = true;
  console.log(index, row)
}
const handleDelete = (index, row) => {
  ElMessageBox.confirm(
      '确认删除该行数据吗?',
      '删除提示',
      {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'error',
      }
  )
      .then(async() => {
        await api.deleteCar(row.id);
        //重新加载数据
        //设置从第一页开始查询
        searchObj.value.current = 1;
        await search();
      })
      .catch(() => {
      })
}
const formatDateTime = (dateTimeStr) => {
  if (!dateTimeStr) return '';
  return dateTimeStr.replace('T', ' ');
}
</script>

<template>
  <el-form :inline="true" :model="searchObj" class="demo-form-inline" style="margin-top:10px">
    <el-form-item label="司机id">
      <el-input v-model="searchObj.driver_id" placeholder="请输入司机id" clearable />
    </el-form-item>
    <el-form-item label="车牌号">
      <el-input v-model="searchObj.car_number" placeholder="请输入车牌号"clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handlerSearch">查询</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handlerAdd">新增</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="车辆id"></el-table-column>
    <el-table-column prop="driver_id" label="司机id"></el-table-column>
    <el-table-column prop="car_number" label="车牌号"></el-table-column>
    <el-table-column prop="car_model" label="车型"></el-table-column>
    <el-table-column prop="car_color" label="车辆颜色"></el-table-column>
    <el-table-column prop="car_brand" label="车辆品牌"></el-table-column>
    <el-table-column prop="register_time" label="注册时间">
      <template #default="scope">
        <span>{{ formatDateTime(scope.row.register_time) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="处理按钮">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
          编辑
        </el-button>
        <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-pagination
      v-model:current-page="searchObj.current"
      v-model:page-size="searchObj.limit"
      :page-sizes="[10,4,30,50]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  />

  <!--编辑对话框-->
  <el-dialog v-model="editWinVisible" title="编辑" width="500">
    <el-form :model="obj">
      <el-form-item label="司机id" :label-width="100">
        <el-input v-model="obj.driver_id" autocomplete="off" />
      </el-form-item>
      <el-form-item label="车牌号" :label-width="100">
        <el-input v-model="obj.car_number" autocomplete="off" />
      </el-form-item>
      <el-form-item label="车型" :label-width="100">
        <el-input v-model="obj.car_model" autocomplete="off" />
      </el-form-item>
      <el-form-item label="车辆颜色" :label-width="100">
        <el-input v-model="obj.car_color" autocomplete="off" />
      </el-form-item>
      <el-form-item label="车辆品牌" :label-width="100">
        <el-input v-model="obj.car_brand" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="editCancel">取消</el-button>
        <el-button type="primary" @click="editCommit">
          提交
        </el-button>
      </div>
    </template>
    <!--  新增对话框-->
  </el-dialog>
  <el-dialog v-model="addWinVisible" title="新增" width="500">
    <el-form :model="obj">
      <el-form-item label="司机id" :label-width="100">
        <el-input v-model="obj.driver_id" autocomplete="off" />
      </el-form-item>
      <el-form-item label="车牌号" :label-width="100">
        <el-input v-model="obj.car_number" autocomplete="off" />
      </el-form-item>
      <el-form-item label="车型" :label-width="100">
        <el-input v-model="obj.car_model" autocomplete="off" />
      </el-form-item>
      <el-form-item label="车辆颜色" :label-width="100">
        <el-input v-model="obj.car_color" autocomplete="off" />
      </el-form-item>
      <el-form-item label="车辆品牌" :label-width="100">
        <el-input v-model="obj.car_brand" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addCancel">取消</el-button>
        <el-button type="primary" @click="addCommit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style scoped></style>